package com.gp.testonline.service.impl;

import com.gp.testonline.dao.StudentMapper;
import com.gp.testonline.eo.StudentEOPage;
import com.gp.testonline.model.Student;
import com.gp.testonline.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 业务接口的实现
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 添加学生信息
     * @param student 学生实体
     * @return
     */
    @Override
    public int InsertStudent(Student student) {
        int isInsert=0;
        /*
         * 学号不能重复
         */
        int studentIdCount=studentMapper.getStudentIdCount(student.getStudentid());
        if (studentIdCount<1){
            isInsert=studentMapper.insertStudent(student);
        }else {
            isInsert=-1;//有相同的学号
        }
        return isInsert;
    }

    @Override
    public int UpdateStudent(Student student) {
        int isUpdate=0;
        /*1 没有相关业务流程的判断*/
        try{
            isUpdate=studentMapper.updateStudent(student);
        }catch (Exception e){
            isUpdate=-1;
            //打印更新时报错的情况
            System.out.println("更新学生信息时执行sql报错的原因为："+e.getMessage());
        }
        return isUpdate;
    }

    @Override
    public int DeleteStudent(int sid) {
        int isDelete=0;
        /*1 没有相关业务流程的判断*/
        try{
            isDelete=studentMapper.deleteStudent(sid);
        }catch (Exception e){
            isDelete=-1;
            //打印更新时报错的情况
            System.out.println("删除学生信息时执行sql报错的原因为："+e.getMessage());
        }
        return isDelete;
    }

    @Override
    public List<StudentEOPage> getStudentAll(StudentEOPage studentEOPage) {
        List<StudentEOPage> listStudentEo=studentMapper.getStudentAll(studentEOPage);
        return listStudentEo;
    }
}
